package com.wy.thread_pool;

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 美女
 * @date 2022/05/03 14:44
 **/
public class Demo3 {
    static class CalcFib implements Runnable{
        private final int n;

         CalcFib(int n) {
            this.n = n;
        }

        @Override
        public void run() {
             long r=fib(n);
            System.out.printf("fib(%d)=%d\n",n,r);
        }
        private long fib(int n){
            if(n==0||n==1){
                return 1;
            }
            return fib(n-1)+fib(n-2);
        }
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        ExecutorService service= Executors.newFixedThreadPool(10);//创建了一个固定长度的线程池
        while(true){
            System.out.print("提交数字：");
            int n=scanner.nextInt();
            Runnable task=new CalcFib(n);//这个线程执行的任务是计算n的斐波那契数
            service.execute(task);//将任务传入线程池
        }
    }
}
